package com.ef.web.action;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.Attachment;
import com.ef.domain.FAQ;
import com.ef.service.AttachmentService;
import com.ef.service.FAQService;
import com.ef.util.CustomExample;

public class FAQAction extends BaseAction<FAQ> {

	private static final long serialVersionUID = 1L;
	private FAQService fAQService;
	private AttachmentService attachmentService;
	private List<Attachment> attachs;

	 

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(FAQAction.class);

	@SuppressWarnings("serial")
	public String list() {

		this.listResult = fAQService.findByExample(new CustomExample<FAQ>(this
				.getModel()) {

			public void appendCondition(Criteria criteria) {
				this.enableLike(MatchMode.ANYWHERE);
			}
		}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {
		FAQ faq = this.getModel();
		if (faq != null && faq.getId() != null) {
			attachs = attachmentService.findByFAQ(faq);
		}
		return EDIT;
	}

	public String save() {

		FAQ faq = this.getModel();
		faq.setUpdateTime(new Date());
		faq.setUpdateUser(getSessionUser());
		fAQService.saveOrUpdate(faq);
		String attachmentIds = getRequest().getParameter("attachmentIds");
		if (attachmentIds != null && !attachmentIds.trim().equals("")) {
			String[] ids = attachmentIds.split(",");
			for (String id : ids) {
				if(!id.trim().equals("")){
				Attachment a = attachmentService.findById(Long.valueOf(id));
				a.setFaq(faq);
				attachmentService.saveOrUpdate(a);
				}
			}
		}
		this.setResult(ConfirmMessage.FAQ);
		if (faq != null && faq.getId() != null) {
			attachs = attachmentService.findByFAQ(faq);
		}
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			fAQService.delete(fAQService.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public FAQService getfAQService() {
		return fAQService;
	}

	public void setfAQService(FAQService fAQService) {
		this.fAQService = fAQService;
	}

	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	public List<Attachment> getAttachs() {
		return attachs;
	}

	public void setAttachs(List<Attachment> attachs) {
		this.attachs = attachs;
	}

}
